package com.heyu.cgsd.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.heyu.cgsd.R;
import com.heyu.cgsd.ui.activity.MainActivity;
import com.heyu.cgsd.util.ClassUtil;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link SelectFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class SelectFragment extends Fragment {
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";

    private View view;
    private int index;
    private MainActivity mActivity;
    private LinearLayout ll_select_1;
    private LinearLayout ll_select_2;
    private LinearLayout ll_select_3;
    private LinearLayout ll_select_4;
    private LinearLayout ll_select_5;
    private LinearLayout ll_select_6;
    private LinearLayout ll_select_7;
    private LinearLayout ll_select_8;

    public SelectFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @return A new instance of fragment ReadFragment.
     */
    public static SelectFragment newInstance(int index) {
        SelectFragment fragment = new SelectFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_PARAM1, index);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            index = getArguments().getInt(ARG_PARAM1);
        }
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if (index == 0) {
            view = inflater.inflate(R.layout.fragment_select_1, container, false);
        }

        initView();
        // Inflate the layout for this fragment
        mActivity = (MainActivity) getActivity();
        updateData();
        initListener();
        return view;
    }

    private void initView() {
        if (index == 0) {
            ll_select_1 = view.findViewById(R.id.ll_select_1);
            ll_select_2 = view.findViewById(R.id.ll_select_2);
            ll_select_3 = view.findViewById(R.id.ll_select_3);
            ll_select_4 = view.findViewById(R.id.ll_select_4);
            ll_select_5 = view.findViewById(R.id.ll_select_5);
            ll_select_6 = view.findViewById(R.id.ll_select_6);
            ll_select_7 = view.findViewById(R.id.ll_select_7);
            ll_select_8 = view.findViewById(R.id.ll_select_8);
        }
    }

    public void updateData() {

    }

    private void initListener() {
        if (index == 0) {
            ll_select_1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    gotoActivityByIndex(1);
                }
            });

            ll_select_2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    gotoActivityByIndex(2);
                }
            });

            ll_select_3.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    gotoActivityByIndex(3);
                }
            });

            ll_select_4.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    gotoActivityByIndex(4);
                }
            });

            ll_select_5.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    gotoActivityByIndex(5);
                }
            });

            ll_select_6.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    gotoActivityByIndex(6);
                }
            });

            ll_select_7.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    gotoActivityByIndex(7);
                }
            });

            ll_select_8.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    gotoActivityByIndex(8);
                }
            });
        }
    }

    private void gotoActivityByIndex(int index) {
        mActivity.startActivity(new Intent(mActivity, ClassUtil.getClassList()[index - 1]));
    }
}
